﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TravelWeb.Models;

namespace TravelWeb.Controllers
{
    public class ScheduleController : Controller
    {
        private TravelWebDB db = new TravelWebDB();

        //
        // GET: /Schedule/

        public ViewResult Index(int tourID)
        {
            var schedules = db.Schedules.Include(s => s.Tour).Include(s => s.Transportation).Include(s => s.Promotion);
            List<Schedule> list = new List<Schedule>();
            if (tourID > 0)
            {
                foreach (var item in schedules.ToList())
                {
                    if (item.TourID == tourID)
                    {
                        list.Add(item);
                    }
                }
                return View(list);
            }
            return View(schedules.ToList());
        }

        //
        // GET: /Schedule/Details/5

        public ViewResult Details(int id)
        {
            Schedule schedule = db.Schedules.Find(id);
            return View(schedule);
        }

        //
        // GET: /Schedule/Create

        public ActionResult Create(int tourID, int? promotionID)
        {
            ViewBag.TourID = new SelectList(db.Tours, "TourID", "TourName");
            ViewBag.TransportationID = new SelectList(db.Transportations, "TransportationID", "TransportationName");
            ViewBag.PromotionID = new SelectList(db.Promotions, "PromotionID", "PromotionName");

            Schedule schedule = new Schedule()
            {
                TourID = tourID,
                ConfirmDate = DateTime.Now,
                StartDate = DateTime.Now,
                EndDate = DateTime.Now,
                Status = "New"
            };
            if (promotionID != null)
                schedule.PromotionID = promotionID.Value;

            return View(schedule);
        }

        //
        // POST: /Schedule/Create

        [HttpPost]
        public ActionResult Create(Schedule schedule)
        {
            if (ModelState.IsValid)
            {
                db.Schedules.Add(schedule);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.TourID = new SelectList(db.Tours, "TourID", "TourName", schedule.TourID);
            ViewBag.TransportationID = new SelectList(db.Transportations, "TransportationID", "TransportationName", schedule.TransportationID);
            ViewBag.PromotionID = new SelectList(db.Promotions, "PromotionID", "PromotionName", schedule.PromotionID);
            return View(schedule);
        }

        //
        // GET: /Schedule/Edit/5

        public ActionResult Edit(int id)
        {
            Schedule schedule = db.Schedules.Find(id);
            ViewBag.TourID = new SelectList(db.Tours, "TourID", "TourName", schedule.TourID);
            ViewBag.TransportationID = new SelectList(db.Transportations, "TransportationID", "TransportationName", schedule.TransportationID);
            ViewBag.PromotionID = new SelectList(db.Promotions, "PromotionID", "PromotionName", schedule.PromotionID);
            return View(schedule);
        }

        //
        // POST: /Schedule/Edit/5

        [HttpPost]
        public ActionResult Edit(Schedule schedule)
        {
            if (ModelState.IsValid)
            {
                db.Entry(schedule).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.TourID = new SelectList(db.Tours, "TourID", "TourName", schedule.TourID);
            ViewBag.TransportationID = new SelectList(db.Transportations, "TransportationID", "TransportationName", schedule.TransportationID);
            ViewBag.PromotionID = new SelectList(db.Promotions, "PromotionID", "PromotionName", schedule.PromotionID);
            return View(schedule);
        }

        //
        // GET: /Schedule/Delete/5

        public ActionResult Delete(int id)
        {
            Schedule schedule = db.Schedules.Find(id);
            return View(schedule);
        }

        //
        // POST: /Schedule/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            Schedule schedule = db.Schedules.Find(id);
            //db.Schedules.Remove(schedule);
            schedule.Status = "Canceled";
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        public ActionResult Approve(int id)
        {
            Schedule schedule = db.Schedules.Find(id);
            ViewBag.TourID = new SelectList(db.Tours, "TourID", "TourName", schedule.TourID);
            ViewBag.TransportationID = new SelectList(db.Transportations, "TransportationID", "TransportationName", schedule.TransportationID);
            ViewBag.PromotionID = new SelectList(db.Promotions, "PromotionID", "PromotionName", schedule.PromotionID);
            if (schedule.TransportationID != null)
            {
                var _tran = from tran in db.Transportations
                          where tran.TransportationID == schedule.TransportationID.Value
                          select tran;
                var bus = _tran.FirstOrDefault();
                if (!bus.Status.Equals("Busy"))
                {
                    schedule.Status = "Approved";
                    db.SaveChanges();
                    return View(schedule);
                }
            }
            return View("BusBusy");
        }

        public ActionResult Booking(int tourID, int scheduleID, int promotionID)
        {
            var orders = from o in db.Orders
                         where o.TourID == tourID
                         && o.ScheduleID == scheduleID
                         && o.PromotionID == promotionID
                         select o;
            foreach (var item in orders)
            {
                if (item.Customer.Username.Equals(User.Identity.Name))
                {
                    return RedirectToAction("OrderExisted");
                }
            }
            return RedirectToAction("Create", "Order", new OrderKey() { TourID = tourID, ScheduleID = scheduleID,
            PromotionID = promotionID});
        }

        public ActionResult BusBusy()
        {
            return View();
        }

        public ActionResult OrderExisted()
        {
            return View();
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }

    }
}